<!-- numsidentify.vue -->
<template>
  <div class="s-canvas">
    <canvas id="s-canvas" :width="contentWidth" :height="contentHeight"></canvas>
  </div>
</template>

<script>
export default {
  name: 'NumsIdentify',
  props: {
    fontSizeMin: {
      type: Number,
      default: 25
    },
    fontSizeMax: {
      type: Number,
      default: 30
    },
    backgroundColorMin: {
      type: Number,
      default: 255
    },
    backgroundColorMax: {
      type: Number,
      default: 255
    },
    colorMin: {
      type: Number,
      default: 0
    },
    colorMax: {
      type: Number,
      default: 160
    },
    lineColorMin: {
      type: Number,
      default: 100
    },
    lineColorMax: {
      type: Number,
      default: 255
    },
    dotColorMin: {
      type: Number,
      default: 0
    },
    dotColorMax: {
      type: Number,
      default: 255
    },
    contentWidth: {
      type: Number,
      default: 130
    },
    contentHeight: {
      type: Number,
      default: 30
    }
  },
  data() {
    return {
      identifyCode: '',
    };
  },
  methods: {
    // 生成一个随机数
    randomNum(min, max) {
      return Math.floor(Math.random() * (max - min + 1) + min)
    },
    // 生成一个随机的颜色
    randomColor(min, max) {
      let r = this.randomNum(min, max)
      let g = this.randomNum(min, max)
      let b = this.randomNum(min, max)
      return 'rgb(' + r + ',' + g + ',' + b + ')'
    },
    generateIdentifyCode() {
      const num1 = this.randomNum(1, 20);
      const num2 = this.randomNum(1, 20);
      const operator = Math.random() < 0.5 ? '+' : '-';
      let result = 0;
      
      if (operator === '-') {
        if (num1 < num2) {
          result = num2 - num1;
          this.identifyCode = `${num2}${operator}${num1}=?`;
        } else {
          result = num1 - num2;
          this.identifyCode = `${num1}${operator}${num2}=?`;
        }
      } else {
        result = num1 + num2;
        this.identifyCode = `${num1}${operator}${num2}=?`;
      }
      
      this.result = result.toString();
      this.$emit('update:identifyCode', this.identifyCode);
      this.$emit('update:result', this.result);
    },
    // 绘制验证码
    drawPic() {
      let canvas = document.getElementById('s-canvas')
      let ctx = canvas.getContext('2d')
      ctx.textBaseline = 'bottom'
      ctx.fillStyle = this.randomColor(this.backgroundColorMin, this.backgroundColorMax)
      ctx.fillRect(0, 0, this.contentWidth, this.contentHeight)
      this.drawText(ctx, this.identifyCode)
      this.drawLine(ctx)
      this.drawDot(ctx)
    },
    // 绘制文字
    drawText(ctx, txt) {
      ctx.fillStyle = this.randomColor(this.colorMin, this.colorMax)
      ctx.font = `bold ${this.randomNum(this.fontSizeMin, this.fontSizeMax)}px Arial`
      const textMetrics = ctx.measureText(txt);
      const x = (this.contentWidth - textMetrics.width) / 2;
      const y = this.contentHeight * 1;
      ctx.fillText(txt, x, y)
      ctx.textBaseline = 'middle'
    },
    // 绘制干扰线
    drawLine(ctx) {

      for (let i = 0; i < 5; i++) {
        ctx.strokeStyle = this.randomColor(this.lineColorMin, this.lineColorMax)
        ctx.beginPath()
        ctx.moveTo(this.randomNum(0, this.contentWidth), this.randomNum(0, this.contentHeight))
        ctx.lineTo(this.randomNum(0, this.contentWidth), this.randomNum(0, this.contentHeight))
        ctx.stroke()
      }
    },
    // 绘制干扰点
    drawDot(ctx) {

      for (let i = 0; i < 80; i++) {
        ctx.fillStyle = this.randomColor(0, 255)
        ctx.beginPath()
        ctx.arc(this.randomNum(0, this.contentWidth), this.randomNum(0, this.contentHeight), 1, 0, 2 * Math.PI)
        ctx.fill()
      }
    }
  },
  watch: {
    identifyCode() {
      this.drawPic()
    }
  },
  mounted() {
    this.generateIdentifyCode()
    this.drawPic()
  }
}
</script>

<style scoped>
.s-canvas {
  height: 30px;
}

.s-canvas canvas {
  margin-left: 8px;
}
</style>
